import { TextNode, TextNodeModel, h } from "@logicflow/core";

class CustomTextView extends TextNode {
    // 自定义节点外观
    getShape() {
        const { model } = this.props;
        const { x, y, width, height } = model;
        const style = model.getNodeStyle();

        return h('g', {}, [
            h('rect', {
                ...style,
                x: x - width / 2,
                y: y - height / 2,
                width,
                height
            })
            // 移除手动渲染文本的代码，让 LogicFlow 默认渲染
        ]);
    }
}

class CustomTextModel extends TextNodeModel {
    constructor(data, graphModel) {
        // 确保 data.text 存在，若不存在则设为空字符串
        const textValue = typeof data.text === 'object' ? data.text.value : data.text || '文本标签';
        data.text = {
            value: textValue,
            x: data.x,
            y: data.y
        };
        // 设置默认宽度和高度
        data.width = 300;
        data.height = 30;
        // 只传递 data 参数
        super(data, graphModel);
    }

    // 设置节点样式
    getNodeStyle() {
        const style = super.getNodeStyle();
        style.stroke = '#b2b2b2';
        style.fill = '#ffffff';
        style.strokeWidth = 1;
        return style;
    }

    // 设置文本样式
    getTextStyle() {
        const style = super.getTextStyle();
        style.fontSize = 14;
        style.fill = '#000000';
        return style;
    }
}

export default {
    type: 'custom-text',
    view: CustomTextView,
    model: CustomTextModel,
};